package cn.donting.jme.utile;

import com.jme3.material.Material;
import com.jme3.scene.Mesh;
import com.jme3.scene.Spatial;
import com.jme3.util.clone.CloneFunction;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.IdentityCloneFunction;

public class CloneUtil {

    public static <T extends Spatial> T clone(T spatial,boolean cloneMaterial){
        Cloner cloner = new Cloner();
        // Set up the cloner for the type of cloning we want to do.

        // First, we definitely do not want to clone our own parent
        cloner.setClonedValue(spatial.getParent(), null);

        // If we aren't cloning materials then we will make sure those
        // aren't cloned also
        if (!cloneMaterial) {
            cloner.setCloneFunction(Material.class, new IdentityCloneFunction<Material>());
        }

        // Clone it!
        T clone= cloner.clone(spatial);

        // Because we've nulled the parent out we need to make sure
        // the transforms and stuff get refreshed.
        clone.forceRefresh(true,true,true);

        return clone;
    }
}
